<?php
class SitecheckAction extends CommonAction {
    //检测所有项目
	public function index(){
		$list[] = $this->CheckCategory();
		$list[] = $this->checkWeixinConfig();
		$list[] = $this->checkSiteConfig();
		$list[] = $this->checkPaymentAndAffConfigAndBonusConfig();
		$this->assign('list', $list);
		$this->display();
	}
	
	//检测栏目是否为空
	public function CheckCategory(){
		$catedb = D('Category');
		$cateCount = $catedb -> where(array('site_id' => $this->siteid)) -> count();
		$this->assign('cateCount', $cateCount);
		$cateList = $catedb -> where(array('site_id' => $this->siteid, 'pid' => 0)) -> select();
		$errorlist = array();
		$pagedb = D('Pages');
		foreach($cateList as $k => $v){
			//$pages['page_list_header_id'] = $pagedb -> where(array('site_id' => $this->siteid, 'id' => $v['page_list_header_id'])) -> find();
			$pages['page_list_id'] = $pagedb -> where(array('site_id' => $this->siteid, 'id' => $v['page_list_id'])) -> find();
			//$pages['page_list_footer_id'] = $pagedb -> where(array('site_id' => $this->siteid, 'id' => $v['page_list_footer_id'])) -> find();
			//$pages['page_view_header_id'] = $pagedb -> where(array('site_id' => $this->siteid, 'id' => $v['page_view_header_id'])) -> find();
			$pages['page_view_id'] = $pagedb -> where(array('site_id' => $this->siteid, 'id' => $v['page_view_id'])) -> find();
			//$pages['page_view_footer_id'] = $pagedb -> where(array('site_id' => $this->siteid, 'id' => $v['page_view_footer_id'])) -> find();
			if(!$pages['page_list_id'] || !$pages['page_view_id']){
				$errorlist[] = $v;
			}//dump($pagedb -> getLastSql());
		}
		$this->assign('errorlist', $errorlist);
		return $this->fetch('CheckCategory');
	}
	
	//检测微信设置
	public function checkWeixinConfig(){
		$d['token']=getSysVal('token');
        $d['appid']=getSysVal('appid');
        $d['appsecret']=getSysVal('appsecret');
		//检测菜单
		$Weixin_menudb = M('Weixin_menu');
		$Weixin_menuCount = $Weixin_menudb -> where(array('site_id' => $this->siteid, 'pid' => 0)) -> count();
		$this->assign('Weixin_menuCount', $Weixin_menuCount);
		if(!$d['appsecret'] || !$d['appid'] || !$d['token']){
			$this->assign('noconfig', true);
		}else{
			$this->assign('noconfig', false);
		}
		return $this->fetch('checkWeixinConfig');
	}
	
	//检测站点设置
	public function checkSiteConfig(){
		$this->dao = D('Site');
		$ConfList=$this->dao->alias("s")->join("left join __TEMPLATE__ as t on t.id=s.template_id")->where(array('s.id'=>$this->siteid))->Field("t.config as confList")->find();
		//$this->setting_list = array();
		$setting_list=json_decode($ConfList['confList'],true);
		$this->id=isset($_GET['id'])?$this->_get('id'):$this->siteid;
		//多表存储扩展,提高效率
		$this->cur_setting = M('Setting_extends')->where(array('site_id'=>$this->id))->select();
		foreach($this->cur_setting as $k=>$v){
			$cur_setting[$v['name']]=$v['val'];
		}
		
		foreach($setting_list as $k => $v){
			foreach($v['list'] as $key => $val){
				$setting_list[$k]['list'][$key]['val'] = $cur_setting[$val['name']];
				//dump($setting_list[$k]['list'][$key]);
			}
		}
		//dump($setting_list);
		$this->assign('setting_list',$setting_list);
		
		
		//检测菜单
		return $this->fetch('checkSiteConfig');
	}
	
	//检测支付接口 分成规则 红包设置
	public function checkPaymentAndAffConfigAndBonusConfig(){
		$sitedb = D('Site');
		$site = $sitedb -> where(array('id' => $this->siteid)) -> find();
		$bonusconfig = unserialize($site['bonusconfig']);
		$qrcodesetting = unserialize($site['qrcodesetting']);
		$this->assign('qrcodesetting', $qrcodesetting);
		//推广二维码
		if(!is_file($qrcodesetting['qrbackground'])){
			$this->assign('qrbackground', false);
		}else{
			$this->assign('qrbackground', true);
		}
		//关注链接
		
        //$linkstatus = get_headers($qrcodesetting['subscribeLink']);
		//红包证书和相关配置
		
		
		$this->assign('bonusconfig', $bonusconfig);
		 
		//分成设置
		$aff = M('Affiliatesetting') -> where(array('status' => 1, 'site_id' => $this->siteid)) -> find();
		$this->assign('aff', $aff);
		return $this->fetch('checkPaymentAndAffConfigAndBonusConfig');
	}
	
	//显示设置向导
	public function showsetup(){
		//设置步骤
		$step = isset($_GET['step']) ? intval($_GET['step']) : 0;
		
		
		
		$setup = array();
		if(getAccessMenu($this->accessList, 'Admin', 'Site', 'Conf')){
			$setup[] = array('title' => '基本设置', 'url' => U('Site/Conf'));
		}
		
		if(getAccessMenu($this->accessList, 'Admin', 'Payment', 'index')){
			$setup[] = array('title' => '支付接口设置', 'url' => U('Payment/index'));
		}
		
		if(getAccessMenu($this->accessList, 'Admin', 'Affiliatesetting', 'index')){
			$setup[] = array('title' => '分成规则设置', 'url' => U('Affiliatesetting/index'));
		}
		
		if(getAccessMenu($this->accessList, 'Admin', 'Affiliatesetting', 'setQrcode')){
			$setup[] = array('title' => '推广二维码设置', 'url' => U('Affiliatesetting/setQrcode'));
		}
		
		if(getAccessMenu($this->accessList, 'Admin', 'detail', 'add')){
			$setup[] = array('title' => '提现设置', 'url' => U('detail/add'));
		}
		
		if(getAccessMenu($this->accessList, 'Admin', 'Weixin_menu', 'index')){
			$setup[] = array('title' => '微信自定义菜单设置', 'url' => U('Weixin_menu/index'));
		}
		
		if(getAccessMenu($this->accessList, 'Admin', 'Reply', 'index')){
			$setup[] = array('title' => '微信自动回复设置', 'url' => U('Reply/index'));
		}
		//总步骤数值
		$this -> setupcount = count($setup);
	    
		
		if($step > 0){
		    $this->prevurl = U('', array('step' => $step - 1));
		}
		
		$this->step = $step + 1;
		
		if($this->step < $this -> setupcount){
		    $this->nexturl = U('', array('step' => $this->step));
		}
		$this->setup = $setup[$step];
		$this->display();
	}
} 